<?php

namespace app\api\controller;

use app\api\model\page\Page as AppPage;
use app\api\model\plus\article\Article;
use app\api\model\settings\Setting as SettingModel;
use app\common\model\app\AppUpdate as AppUpdateModel;
use app\common\model\supplier\Service as ServiceModel;
use app\common\model\app\AppOpen as AppOpenModel;
/**
 * 页面控制器
 */
class Index extends Controller
{
    /**
     * 首页
     */
    public function index($page_id = null)
    {
        // 页面元素
        $data = AppPage::getPageData($this->getUser(false), $page_id);
        $data['setting'] = array(
            'collection' => SettingModel::getItem('collection'),
            'officia' => SettingModel::getItem('officia'),
            'homepush' => SettingModel::getItem('homepush'),
        );
        return $this->renderSuccess('', $data);
    }

    // 公众号客服
    public function mpService($shop_supplier_id)
    {
        $mp_service = ServiceModel::detail($shop_supplier_id);
        return $this->renderSuccess('', compact('mp_service'));
    }
    //底部导航
    public function nav(){
       $data = SettingModel::getItem('nav');
       return $this->renderSuccess('', $data); 
    }

    /**
     * 通用配置
     * @return [type] [description]
     */
    public function config()
    {
        $config = SettingModel::getItem('store');
        return $this->renderSuccess('', $config); 
    }

    /**
     * APP下载页面
     * @return [type] [description]
     */
    public function getAppDownload()
    {
        $url = base_url().'/app/lianhela.dartrun.com.apk';
        $logo = AppOpenModel::detail()['logo'];
        $data = [
            'logo' => $logo,
            'url'  => $url,
        ];
        return $this->renderSuccess('', $data);
    }


    // app更新
    public function update($name, $version, $platform){
        $result = [
            'update' => false,
            'wgtUrl' => '',
            'pkgUrl' => '',
        ];
        try {
            $model = AppUpdateModel::getLast();
            // 这里简单判定下，不相等就是有更新。
            if($model && $version != $model['version']){
                $currentVersions = explode('.', $version);
                $resultVersions = explode('.', $model['version']);

                if ($currentVersions[0] < $resultVersions[0]) {
                    // 说明有大版本更新
                    $result['update'] = true;
                    $result['pkgUrl'] = $platform == 'android'?$model['pkg_url_android']:$model['pkg_url_ios'];
                } else {
                    // 其它情况均认为是小版本更新
                    $result['update'] = true;
                    $result['wgtUrl'] = $model['wgt_url'];
                }
            }
        } catch (\Exception $e) {

        }
        return $this->renderSuccess('', compact('result'));
    }

    public function get_article()
    {
        $id = $this->request->param('article_id');
        $model = new Article();
        $article = $model->field("article_content")->find($id);
        echo $article['article_content'];

    }
}
